<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
		<link rel="stylesheet" href="./layui/css/layui.css">
		<script type="text/javascript" src="js/global.js"></script>
		<script src="./layui/layui.js"></script>
	</head>

	<body>
		<div class="layui-form-item" style="margin-top: 20px">
			<label class="layui-form-label">模块名：</label>
			<div class="layui-input-inline" style="width: 350px">
				<input type="text" name="name" id="name" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">父级模块：</label>
			<div class="layui-input-inline" style="width: 350px">
				<select class="layui-input" name="parentId" id="parentId" lay-verify="required">
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">请求地址：</label>
			<div class="layui-input-inline" style="width: 350px">
				<input type="text" name="path" id="path" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">权重：</label>
			<div class="layui-input-inline" style="width: 350px">
				<input type="text" name="weight" id="weight" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-btn-container" style="text-align: center;">
				<button class="layui-btn" lay-submit lay-filter="formDemo">添加</button>
			</div>
		</div>

		<script>
			//Demo
			layui.use(['form'], function() {
				var form = layui.form;
				var $ = layui.jquery;
				//解决跨域
				$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
					options.xhrFields = {
						withCredentials: true
					}
				})

				function getType() {
					$.getJSON(globalData.server + "Modules/getAllParentId", function(obj) {
						var option = "";
						for(i = 0; i < obj.length; i++) {
							option += "<option value='" + obj[i].id + "'>" + obj[i].name + "</option>";
						}
						$("#parentId").html('<option value="0">--- 请选择 ---</option>' + option);
						console.log(option);
					});
				}
				getType();
				//监听提交
				form.on('submit(formDemo)', function(data) {
					var d = {
						'name': $("#name").val(),
						'parentId': $("#parentId").val(),
						'path': $("#path").val(),
						'weight': $("#weight").val(),
					}; //表单中的数据，定义一个对象来接收
					var url = globalData.server + './Modules/add';
					$.post(url, d, function(data) {
						console.log(data);
						if(data.count > 0) {
							var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
							parent.layer.close(index); //再执行关闭   
						} else {
							layer.msg(data.msg, {
								icon: 6
							});
						}
					}, "json");
					return false;
				});
			});
		</script>
	</body>

</html>